﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class UMDao
    {


        public UMDao()
        {

            Mapper.CreateMap<Y_UM, UMModel>();
        }
        public List<Y_UM> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_UM.Count();

                return edm.Y_UM.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public UMModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_UM.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_UM, UMModel>(query);
            }

        }
        public UMModel GetOneByUId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_UM.FirstOrDefault(s => s.UserId == id);
                return Mapper.Map<Y_UM, UMModel>(query);
            }
        }

        public int Save(UMModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_UM.FirstOrDefault(s => s.UserId == obj.UserId);

                if (query == null)
                {


                    query = new Y_UM();

                    edm.AddToY_UM(query);

                }


                query.UserId = obj.UserId;

                query.MudleId = obj.MudleId;

                query.DepartId = obj.DepartId;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_UM.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

